package com.lzh.dlykserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzh.dlykserver.constant.Constants;
import com.lzh.dlykserver.entity.TClueRemark;
import com.lzh.dlykserver.entity.TCustomerRemark;
import com.lzh.dlykserver.mapper.TCustomerRemarkMapper;
import com.lzh.dlykserver.query.CustomerQuery;
import com.lzh.dlykserver.service.CustomerRemarkService;
import com.lzh.dlykserver.util.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class CustomerRemarkServiceImpl implements CustomerRemarkService {

    @Resource
    private TCustomerRemarkMapper tCustomerRemarkMapper;


    @Override
    public PageInfo<TClueRemark> getCustomerRemarkByPage(Integer current, CustomerQuery customerQuery) {
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        List<TClueRemark> tClueRemarkList = tCustomerRemarkMapper.selectCustomerRemarkByPage(customerQuery);
        PageInfo<TClueRemark> info = new PageInfo<>(tClueRemarkList);
        return info;
    }

    @Override
    public int saveaClueRemark(CustomerQuery customerQuery) {
        TCustomerRemark tCustomerRemark = new TCustomerRemark();
        BeanUtils.copyProperties(customerQuery, tCustomerRemark);

        Integer loginUserId = JWTUtils.parseUserFromJWT(customerQuery.getToken()).getId();

        tCustomerRemark.setCreateBy(loginUserId);
        tCustomerRemark.setCreateTime(new Date());

        return tCustomerRemarkMapper.insertSelective(tCustomerRemark);
    }

    @Override
    public TCustomerRemark getEditedCustomerRemarkById(Integer id) {
        TCustomerRemark tCustomerRemark = tCustomerRemarkMapper.selectByPrimaryKey(id);
        return tCustomerRemark;
    }

    @Override
    public int editCustomerRemark(CustomerQuery customerQuery) {
        TCustomerRemark tCustomerRemark = new TCustomerRemark();
        BeanUtils.copyProperties(customerQuery, tCustomerRemark);

        Integer loginUserId = JWTUtils.parseUserFromJWT(customerQuery.getToken()).getId();

        tCustomerRemark.setCreateBy(loginUserId);
        tCustomerRemark.setCreateTime(new Date());
        return tCustomerRemarkMapper.updateByPrimaryKeySelective(tCustomerRemark);
    }

    @Override
    public int delCustomerMark(Integer id) {
        // 逻辑删除
        TCustomerRemark tCustomerRemark = new TCustomerRemark();
        tCustomerRemark.setId(id);
        tCustomerRemark.setDeleted(1);
        return tCustomerRemarkMapper.updateByPrimaryKeySelective(tCustomerRemark);
    }


}
